# docker run --rm -v "$PWD":/usr/src/myapp -e GOPROXY="https://goproxy.io,direct" -w /usr/src/myapp af1d71e5b598 mage -v

RELATIVE_DIR=""
function now_dir() {
    DIRS=("" "../")
    for D in ${DIRS[*]}; do
        if [ -f "${D}version" ]; then
            RELATIVE_DIR=${D}
            break
        fi
    done
}

now_dir
EXEC_NAME=$(grep '  "executable":' ${RELATIVE_DIR}src/plugin.json | awk '{ print $2}')
EXEC_NAME=$(echo ${EXEC_NAME} | sed 's/,/\n/g')
EXEC_NAME=$(echo ${EXEC_NAME} | sed 's/\"//g')
echo "executable name:" ${EXEC_NAME}

if [ "${RELATIVE_DIR}" != "" ]; then
    cd ${RELATIVE_DIR}
fi

OUT_DIR=${RELATIVE_DIR}dist/

echo "build target dir:" ${OUT_DIR}

docker run --rm -v "$PWD":/usr/src/myapp -e GOPROXY="https://goproxy.io,direct" \
    -w /usr/src/myapp golang:1.19 \
    /bin/bash -c \
    "GOOS=linux GOARCH=amd64 go build  -v -o ${OUT_DIR}${EXEC_NAME}_linux_amd64 ./pkg && \
    GOOS=linux GOARCH=arm go build  -v -o ${OUT_DIR}${EXEC_NAME}_linux_arm ./pkg&& \
    GOOS=linux GOARCH=arm64 go build  -v -o ${OUT_DIR}${EXEC_NAME}_linux_arm64 ./pkg && \
    GOOS=darwin GOARCH=amd64 go build  -v -o ${OUT_DIR}${EXEC_NAME}_darwin_amd64 ./pkg && \
    GOOS=darwin GOARCH=arm64 go build  -v -o ${OUT_DIR}${EXEC_NAME}_darwin_arm64 ./pkg && \
    GOOS=windows GOARCH=amd64 go build -v  -o ${OUT_DIR}${EXEC_NAME}_windows_amd64.exe ./pkg"
